package com.kh.statistics.video.core.login;

import com.kh.statistics.video.constant.PlatformConstant;
import com.kh.statistics.video.core.login.entity.KuaiShouHealthCheck;
import com.kh.statistics.video.core.login.entity.KuaiShouHealthCheckResponse;
import com.kh.statistics.video.entity.StatisticsMeta;
import com.kh.statistics.video.entity.VideoAccountsHealth;
import com.kh.statistics.video.entity.VideoAccountsLogin;
import com.kh.statistics.video.helper.CookieHelper;
import com.kh.statistics.video.helper.HttpServletHelper;
import com.kh.statistics.video.helper.StatisticsHttpHelper;
import com.kh.statistics.video.helper.VideoAccountsHealthHelper;
import com.kh.statistics.video.utils.SerializationUtils;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.login
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-13  14:04
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class KuaiShouAccountsLogin implements AccountsLogin {
    @Override
    public String support() {
        return PlatformConstant.KUAI_SHOU;
    }

    @Override
    public Boolean login(VideoAccountsLogin videoAccountsLogin, WebDriver driver) throws IOException, InterruptedException {
        boolean flag = true;
        String content = null;
        while (flag || null == content) {
            driver.get("https://passport.kuaishou.com/pc/account/login/?sid=kuaishou.web.cp.api&callback=https%3A%2F%2Fcp.kuaishou.com%2Frest%2Finfra%2Fsts%3FfollowUrl%3Dhttps%253A%252F%252Fcp.kuaishou.com%252Fprofile%26setRootDomain%3Dtrue");
            Thread.sleep(2000);
            WebElement login = driver.findElement(By.className("platform-switch-tips"));
            login.click();
            Thread.sleep(1000);
            flag = false;
            content = driver.getPageSource();
        }

        HttpServletHelper.writer(content);
        CookieHelper.getCookies(driver, "快手");
        CookieConvertMyCookie.setCookies(driver, videoAccountsLogin);
        WebDriver.Options manage = driver.manage();
        videoAccountsLogin.setCookieSet(CookieConvertMyCookie.CookiesToMyCookies(manage.getCookies()));
        return true;
    }

    @Override
    public VideoAccountsHealth creatorHealthChecks(VideoAccountsLogin videoAccountsLogin) {
        StatisticsMeta statisticsMeta = new StatisticsMeta();
        statisticsMeta.setRequestType("post");
        statisticsMeta.setUrl("https://cp.kuaishou.com/rest/bamboo/pc/hotspot/show");
        KuaiShouHealthCheck kuaiShouHealthCheck = new KuaiShouHealthCheck();
        String cookies = videoAccountsLogin.getCookies();
        String[] split = cookies.split(";");
        for (String item : split) {
            String[] split1 = item.split("=");
            if ("kuaishou.web.cp.api_ph".equals(split1[0])) {
                kuaiShouHealthCheck.setKuaishouWebCpApi(split1[1]);
                break;
            }
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("cookie", cookies);
        statisticsMeta.setHeaders(map);
        statisticsMeta.setJsonParam(SerializationUtils.serialize(kuaiShouHealthCheck));
        String body = StatisticsHttpHelper.request(statisticsMeta);

        KuaiShouHealthCheckResponse response = SerializationUtils.deserialize(body, KuaiShouHealthCheckResponse.class);
        VideoAccountsHealth videoAccountsHealth = VideoAccountsHealthHelper.getVideoAccountsHealth(videoAccountsLogin);
        videoAccountsHealth.setPlatformName("快手");
        if (response.getResult() == 1) {
            log.info("快手健康检查正常:account:{},response:{}", videoAccountsHealth, response);
            videoAccountsHealth.setStatus(1);
        } else {
            log.info("快手健康检查异常：account:{},response:{}", videoAccountsHealth, response);
        }
        return videoAccountsHealth;
    }

    @Override
    public VideoAccountsHealth webHealthChecks(VideoAccountsLogin videoAccountsLogin) {
        return null;
    }
}
